import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export const constantRoutes = [

  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/order',
    component: () => import('@/views/order/index'),
    meta: { title: '用户订单' }
  },
  {
    path: '/orderdetail',
    component: () => import('@/views/orderdetail/index'),
    meta: { title: '订单详情' }
  },
  {
    path: '/user',
    component: () => import('@/views/user/index'),
    meta: { title: '用户' }
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' }
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
